//
//  MBBaseTableViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/19.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBBaseTableViewController.h"
#import "MBBaseTableViewCell.h"

@interface MBBaseTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView; // 实例化的tableView

@end

@implementation MBBaseTableViewController
@dynamic viewModel;

// 调用父类的方法进行初始化ViewModel的传递
- (instancetype)initWithViewModel:(MBBaseViewModel *)viewModel
{
    return [super initWithViewModel:viewModel];
}

-(void)setupLayout
{
    [super setupLayout];
    
    UITableView* tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = COLOR_BG;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
}

-(void)setUpMjRefresh
{
    @weakify(self);
    // 添加头部刷新控件
    MJRefreshNormalHeader * activityHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self pullTableViewRequestData:YES];

    }];
    self.tableView.mj_header = activityHeader;
    [activityHeader setTitle:@"下拉即可刷新" forState:MJRefreshStateIdle];
    [activityHeader setTitle:@"松开即可更新" forState:MJRefreshStatePulling];
    [activityHeader setTitle:@"加载中" forState:MJRefreshStateRefreshing];
    activityHeader.lastUpdatedTimeLabel.hidden = YES;

    
    // 添加尾部刷新控件
    MJRefreshBackNormalFooter * knowledgeFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self pullTableViewRequestData:NO];
    }];
    
    self.tableView.mj_footer = knowledgeFooter;
}

/**
 刷新的时候调用该方法，子类实现，进行重写
 */
- (void)pullTableViewRequestData:(BOOL)isRefresh
{
    
}

- (void)setHideFooterLoadingMore:(BOOL)hideFooterLoadingMore
{
    _hideFooterLoadingMore = hideFooterLoadingMore;
    self.tableView.mj_footer.hidden = hideFooterLoadingMore;
}

- (void)endFooterRefresh
{
    [self.tableView.mj_footer endRefreshing];
}

- (void)endHeaderRefresh
{
    [self.tableView.mj_header endRefreshing];
}

- (Class)cellClassForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @throw [NSException exceptionWithName:@"抽象方法未实现"
                                   reason:[NSString stringWithFormat:@"%@ 必须实现抽象方法 %@",[self class],NSStringFromSelector(_cmd)]
                                 userInfo:nil];
}


#pragma mark - tableView datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.viewModel numberOfSections];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowInSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [self.viewModel heightForHeaderInSection:section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self.viewModel viewForHeaderInSection:section];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [self.viewModel viewForFooterInSection:section];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return [self.viewModel heightForFooterInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBBaseTableViewCell *cell = [[self cellClassForRowAtIndexPath:indexPath] cellForTableView:tableView viewModel:[self.viewModel cellViewModelForRowAtIndexPath:indexPath]];
    cell.selectionStyle = [self.viewModel tableViewCellSelectionStyle];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = tableView.rowHeight;
    NSNumber *calculateHeight = [[self cellClassForRowAtIndexPath:indexPath] calculateRowHeightWithViewModel:[self.viewModel cellViewModelForRowAtIndexPath:indexPath]];
    if (calculateHeight) {
        height = calculateHeight.floatValue;
    }
    return height;
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

@end
